home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / install-datebook.c < prev    next >
C/C++ Source or Header  |  1997-08-07  |  5KB  |  200 lines

  1. /* install-datebook.c:  Pilot datebook installer
  2.  *
  3.  * Copyright 1997, Tero Kivinen
  4.  * Copyright 1996, Robert A. Kaplan (original install-todos.c)
  5.  *
  6.  * This is free software, licensed under the GNU Public License V2.
  7.  * See the file COPYING for details.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "pi-source.h"
  13. #include "pi-socket.h"
  14. #include "pi-dlp.h"
  15. #include "pi-datebook.h"
  16.  
  17. extern time_t parsedate(char * p);
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.   struct pi_sockaddr addr;
  22.   int db;
  23.   int sd;
  24.   int i;
  25.   int Appointment_size;
  26.   unsigned char Appointment_buf[0xffff];
  27.   struct Appointment appointment;
  28.   FILE *f;
  29.   struct PilotUser U;
  30.   int ret;
  31.   int filelen;
  32.   char *cPtr;
  33.   char *file_text;
  34.   char *fields[4];
  35.   int fieldno;
  36.  
  37.   if (argc < 3) {
  38.     fprintf(stderr,"usage:%s %s file [file] ...\n",argv[0],TTYPrompt);
  39.     exit(2);
  40.   }
  41.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  42.     perror("pi_socket");
  43.     exit(1);
  44.   }
  45.     
  46.   addr.pi_family = PI_AF_SLP;
  47.   strcpy(addr.pi_device,argv[1]);
  48.   
  49.   ret = pi_bind(sd, (struct sockaddr*)&addr, sizeof(addr));
  50.   if(ret == -1) {
  51.     perror("pi_bind");
  52.     exit(1);
  53.   }
  54.  
  55.   ret = pi_listen(sd,1);
  56.   if(ret == -1) {
  57.     perror("pi_listen");
  58.     exit(1);
  59.   }
  60.  
  61.   sd = pi_accept(sd, 0, 0);
  62.   if(sd == -1) {
  63.     perror("pi_accept");
  64.     exit(1);
  65.   }
  66.  
  67.   /* Ask the pilot who it is. */
  68.   dlp_ReadUserInfo(sd,&U);
  69.   
  70.   /* Tell user (via Pilot) that we are starting things up */
  71.   dlp_OpenConduit(sd);
  72.   
  73.   /* Open the Datebook's database, store access handle in db */
  74.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "DatebookDB", &db) < 0) {
  75.     puts("Unable to open DatebookDB");
  76.     dlp_AddSyncLogEntry(sd, "Unable to open DatebookDB.\n");
  77.     pi_close(sd);
  78.     exit(1);
  79.   }
  80.  
  81.   for (i=2; i<argc; i++) {
  82.  
  83.     f = fopen(argv[i], "r");
  84.     if (f == NULL) {
  85.       perror("fopen");
  86.       exit(1);
  87.     }
  88.  
  89.     fseek(f, 0, SEEK_END);
  90.     filelen = ftell(f);
  91.     fseek(f, 0, SEEK_SET);
  92.  
  93.     file_text = (char*)malloc(filelen+1);
  94.     if (file_text == NULL) {
  95.       perror("malloc()");
  96.       exit(1);
  97.     }
  98.  
  99.     fread(file_text, filelen, 1, f);
  100.  
  101.     file_text[filelen] = '\0';
  102.     cPtr = file_text;
  103.     fieldno = 0;
  104.     fields[fieldno++] = cPtr;
  105.     while(cPtr - file_text < filelen) {
  106.       if (*cPtr == '\t') {
  107.     if (fieldno >= 4) {
  108.       if (fieldno == 4)
  109.         fprintf(stderr, "Too many fields on the line : %s\n",
  110.             fields[fieldno - 1]);
  111.       fieldno++;
  112.       continue;
  113.     }
  114.     /* replace tab with terminator */
  115.     *cPtr++ = '\0';
  116.     fields[fieldno++] = cPtr;
  117.       } else if (*cPtr == '\n') {
  118.     /* replace cr with terminator */
  119.     *cPtr++ = '\0';
  120.     if (fieldno != 4) {
  121.       fprintf(stderr, "Too few fields : %s", fields[0]);
  122.       fieldno = 0;
  123.       fields[fieldno++] = cPtr;
  124.       continue;
  125.     }
  126.     fieldno = 0;
  127.     if (fields[0][0] == '\0' || fields[1][0] == '\0') { /* no start time */
  128.       appointment.event = 1;
  129.     } else {
  130.       time_t t;
  131.       appointment.event = 0;
  132.       t = parsedate(fields[0]);
  133.       if (t == -1) {
  134.         fprintf(stderr, "Invalid start date or time : %s\n", fields[0]);
  135.         continue;
  136.       }
  137.       appointment.begin = *localtime(&t);
  138.       
  139.       t = parsedate(fields[1]);
  140.       if (t == -1) {
  141.         fprintf(stderr, "Invalid end date or time : %s\n", fields[1]);
  142.         continue;
  143.       }
  144.       appointment.end = *localtime(&t);
  145.     }
  146.     if (fields[2][0] != '\0') {
  147.       appointment.alarm = 1;
  148.       appointment.advance = atoi(fields[2]);
  149.       if (strchr(fields[2], 'm'))
  150.         appointment.advanceUnits = advMinutes;
  151.       else if (strchr(fields[2], 'h'))
  152.         appointment.advanceUnits = advHours;
  153.       else if (strchr(fields[2], 'd'))
  154.         appointment.advanceUnits = advDays;
  155.     } else {
  156.       appointment.alarm = 0;
  157.       appointment.advance = 0;
  158.       appointment.advanceUnits = 0;
  159.     }
  160.     appointment.repeatType = repeatNone;
  161.     appointment.repeatForever = 0;
  162.     appointment.repeatEnd.tm_mday = 0;
  163.     appointment.repeatEnd.tm_mon = 0;
  164.     appointment.repeatEnd.tm_wday = 0;
  165.     appointment.repeatFrequency = 0;
  166.     appointment.repeatWeekstart = 0;
  167.     appointment.exceptions = 0;
  168.     appointment.exception = NULL;
  169.     appointment.description = fields[3];
  170.     appointment.note = NULL;
  171.  
  172.     Appointment_size = pack_Appointment(&appointment, Appointment_buf,
  173.                         sizeof(Appointment_buf));
  174.     printf("desc: %s\n", appointment.description); 
  175.     dlp_WriteRecord(sd, db, 0, 0, 0, Appointment_buf, Appointment_size, 0);
  176.     fields[fieldno++] = cPtr;
  177.       } else {
  178.     cPtr++;
  179.       }
  180.     }
  181.   }
  182.  
  183.   /* Close the database */
  184.   dlp_CloseDB(sd, db);
  185.  
  186.   /* Tell the user who it is, with a different PC id. */
  187.   U.lastSyncPC = 0x00010000;
  188.   U.successfulSyncDate = time(NULL);
  189.   U.lastSyncDate = U.successfulSyncDate;
  190.   dlp_WriteUserInfo(sd,&U);
  191.  
  192.   dlp_AddSyncLogEntry(sd, "Wrote Appointment to Pilot.\n");
  193.   
  194.   /* All of the following code is now unnecessary, but harmless */
  195.   
  196.   dlp_EndOfSync(sd,0);
  197.   pi_close(sd);
  198.   exit(0);
  199. }
  200.